<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.2">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"xiaoqingming18.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.15.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="接口的概念什么是接口接口是Java的一个重要概念，接口定义了一组方法，用于指明实现这个接口的类需要做什么。接口中的方法都是抽象方法，只声明了方法的名称、参数列表和返回类型，没有提供方法的具体实现。 声明接口可以使用interface关键字声明一个接口。声明接口时要注意，接口中所有的方法只能是抽象方法，方法的修饰符通常省略，默认为public abstract。并且，接口中的字段只能是常量，字段的修">
<meta property="og:type" content="article">
<meta property="og:title" content="Java：接口">
<meta property="og:url" content="http://xiaoqingming18.github.io/2023/06/20/Java%EF%BC%9A%E6%8E%A5%E5%8F%A3/index.html">
<meta property="og:site_name" content="鸣蜩十七">
<meta property="og:description" content="接口的概念什么是接口接口是Java的一个重要概念，接口定义了一组方法，用于指明实现这个接口的类需要做什么。接口中的方法都是抽象方法，只声明了方法的名称、参数列表和返回类型，没有提供方法的具体实现。 声明接口可以使用interface关键字声明一个接口。声明接口时要注意，接口中所有的方法只能是抽象方法，方法的修饰符通常省略，默认为public abstract。并且，接口中的字段只能是常量，字段的修">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-06-20T15:08:45.000Z">
<meta property="article:modified_time" content="2023-06-20T15:52:31.983Z">
<meta property="article:author" content="鸣蜩十七">
<meta property="article:tag" content="-Java基础 -接口">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://xiaoqingming18.github.io/2023/06/20/Java%EF%BC%9A%E6%8E%A5%E5%8F%A3/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://xiaoqingming18.github.io/2023/06/20/Java%EF%BC%9A%E6%8E%A5%E5%8F%A3/","path":"2023/06/20/Java：接口/","title":"Java：接口"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Java：接口 | 鸣蜩十七</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>

<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">鸣蜩十七</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">鸣蜩十七是和女朋友谈恋爱的日子</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">接口的概念</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E6%8E%A5%E5%8F%A3"><span class="nav-text">什么是接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A3%B0%E6%98%8E%E6%8E%A5%E5%8F%A3"><span class="nav-text">声明接口</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E6%8E%A5%E5%8F%A3"><span class="nav-text">如何实现接口</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E7%9A%84%E7%89%B9%E6%80%A7%E4%B8%8E%E4%BD%9C%E7%94%A8"><span class="nav-text">接口的特性与作用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E7%9A%84%E7%89%B9%E6%80%A7"><span class="nav-text">接口的特性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-text">接口的作用</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E4%B8%AD%E7%9A%84%E5%85%B6%E4%BB%96%E6%96%B9%E6%B3%95"><span class="nav-text">接口中的其他方法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9D%99%E6%80%81%E5%92%8C%E7%A7%81%E6%9C%89%E6%96%B9%E6%B3%95"><span class="nav-text">静态和私有方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E4%B8%AD%E7%9A%84%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95"><span class="nav-text">接口中的静态方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E4%B8%AD%E7%9A%84%E7%A7%81%E6%9C%89%E6%96%B9%E6%B3%95"><span class="nav-text">接口中的私有方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%BB%98%E8%AE%A4%E6%96%B9%E6%B3%95"><span class="nav-text">默认方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%BB%98%E8%AE%A4%E6%96%B9%E6%B3%95%E5%86%B2%E7%AA%81"><span class="nav-text">默认方法冲突</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E5%9B%9E%E8%B0%83"><span class="nav-text">接口回调</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E6%8E%A5%E5%8F%A3%E5%9B%9E%E8%B0%83"><span class="nav-text">什么是接口回调</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E5%9B%9E%E8%B0%83%E7%9A%84%E6%AD%A5%E9%AA%A4"><span class="nav-text">接口回调的步骤</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E5%9B%9E%E8%B0%83%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-text">接口回调的作用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8E%A5%E5%8F%A3%E5%9B%9E%E8%B0%83%E4%B8%BE%E4%BE%8B"><span class="nav-text">接口回调举例</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">鸣蜩十七</p>
  <div class="site-description" itemprop="description">记录我的学习、生活和恋爱日常</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://xiaoqingming18.github.io/2023/06/20/Java%EF%BC%9A%E6%8E%A5%E5%8F%A3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="鸣蜩十七">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="鸣蜩十七">
      <meta itemprop="description" content="记录我的学习、生活和恋爱日常">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Java：接口 | 鸣蜩十七">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java：接口
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2023-06-20 23:08:45 / 修改时间：23:52:31" itemprop="dateCreated datePublished" datetime="2023-06-20T23:08:45+08:00">2023-06-20</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%90%8E%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">-后端</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="接口的概念"><a href="#接口的概念" class="headerlink" title="接口的概念"></a>接口的概念</h1><h2 id="什么是接口"><a href="#什么是接口" class="headerlink" title="什么是接口"></a>什么是接口</h2><p>接口是Java的一个重要概念，接口定义了一组方法，用于指明实现这个接口的类需要做什么。<strong>接口中的方法都是抽象方法</strong>，只声明了方法的名称、参数列表和返回类型，没有提供方法的具体实现。</p>
<h2 id="声明接口"><a href="#声明接口" class="headerlink" title="声明接口"></a>声明接口</h2><p>可以使用<code>interface</code>关键字声明一个接口。声明接口时要注意，接口中所有的方法只能是抽象方法，方法的修饰符通常省略，默认为<code>public abstract</code>。并且，接口中的字段只能是常量，字段的修饰符默认为<code>public static final</code>，可以省略。</p>
<p>下面是一个声明接口的例子：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">MyInterface</span> {</span><br><span class="line">    <span class="comment">// 声明常量字段</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">MAX_VALUE</span> <span class="operator">=</span> <span class="number">100</span>;</span><br><span class="line">    <span class="comment">// 声明方法</span></span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span>;</span><br><span class="line">    <span class="type">int</span> <span class="title function_">method2</span><span class="params">(String str)</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="如何实现接口"><a href="#如何实现接口" class="headerlink" title="如何实现接口"></a>如何实现接口</h2><p>要想让一个类实现一个接口，需要完成下面两个步骤：</p>
<ol>
<li>用<code>implements</code>关键字将类声明为实现给定的接口，即让类实现某个接口。</li>
<li>在类中<strong>对接口中的所有方法提供定义</strong>。</li>
</ol>
<h1 id="接口的特性与作用"><a href="#接口的特性与作用" class="headerlink" title="接口的特性与作用"></a>接口的特性与作用</h1><h2 id="接口的特性"><a href="#接口的特性" class="headerlink" title="接口的特性"></a>接口的特性</h2><p>接口有以下几个特性：</p>
<ol>
<li>接口中的方法都是隐式地被声明为<code>public abstract</code>，所以在实现接口时，方法不能有具体的实现。</li>
<li>接口不能被实例化，即不能创建接口的对象。但是可以声明接口的变量，并且这个接口变量必须引用实现了这个接口的类对象。</li>
<li>接口可以被其他接口继承，通过关键字<code>extends</code>。一个接口可以继承多个接口。</li>
</ol>
<h2 id="接口的作用"><a href="#接口的作用" class="headerlink" title="接口的作用"></a>接口的作用</h2><ol>
<li>定义规范：接口定义了一组方法，可以作为一种规范或契约，要求实现类按照接口声明的方法来实现。</li>
<li>实现多态：通过接口，可以在不同的类中实现相同的接口方法，从而实现多态的特性。</li>
<li>实现解耦：接口将方法的声明和实现分离，使得代码更加灵活和可维护。</li>
</ol>
<h1 id="接口中的其他方法"><a href="#接口中的其他方法" class="headerlink" title="接口中的其他方法"></a>接口中的其他方法</h1><h2 id="静态和私有方法"><a href="#静态和私有方法" class="headerlink" title="静态和私有方法"></a>静态和私有方法</h2><p>从Java 8开始，接口中可以包含静态方法和私有方法，这为接口的设计和实现带来了更多的灵活性和功能性。</p>
<h3 id="接口中的静态方法"><a href="#接口中的静态方法" class="headerlink" title="接口中的静态方法"></a>接口中的静态方法</h3><p>接口中的静态方法有以下几个特性：</p>
<ul>
<li>接口中的静态方法使用<code>static</code>关键字进行声明。</li>
<li>静态方法可以通过接口名称直接调用，无需实现类的实例。</li>
<li>静态方法不能被实现类所继承或覆盖。</li>
<li>静态方法通常用于提供与接口相关的实用工具方法或公共功能。</li>
</ul>
<p>下面是一个包含静态方法的接口示例：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">MyInterface</span> {</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span>; <span class="comment">// 抽象方法</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">staticMethod</span><span class="params">()</span> {</span><br><span class="line">        <span class="comment">// 静态方法的实现</span></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h3 id="接口中的私有方法"><a href="#接口中的私有方法" class="headerlink" title="接口中的私有方法"></a>接口中的私有方法</h3><ul>
<li>接口中的私有方法使用<code>private</code>关键字进行声明。</li>
<li>私有方法只能在接口内部被调用，用于封装和复用共享逻辑，提高代码的可读性和可维护性。</li>
<li>私有方法可以被默认方法或其他私有方法所调用。</li>
</ul>
<p>下面是一个包含私有方法的接口示例：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">MyInterface</span> {</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span>; <span class="comment">// 抽象方法</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">defaultMethod</span><span class="params">()</span> {</span><br><span class="line">        <span class="comment">// 默认方法的实现</span></span><br><span class="line">        privateMethod();</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">privateMethod</span><span class="params">()</span> {</span><br><span class="line">        <span class="comment">// 私有方法的实现</span></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h2 id="默认方法"><a href="#默认方法" class="headerlink" title="默认方法"></a>默认方法</h2><p>从Java 8开始，接口可以包含默认方法（使用<code>default</code>关键字），这使得接口可以提供方法的默认实现。默认方法可以有方法体，并且实现类可以选择性地覆盖默认方法。</p>
<h2 id="默认方法冲突"><a href="#默认方法冲突" class="headerlink" title="默认方法冲突"></a>默认方法冲突</h2><p>当一个类实现了多个接口，并且这些接口中包含了具有相同签名的默认方法时，就会出现默认方法冲突。为了解决这种冲突，Java接口的默认方法冲突解决机制提供了以下几种规则：</p>
<ol>
<li>类优先原则（Class Wins Rule）：<ul>
<li>如果一个类直接实现了某个接口，并且同时继承了另一个类（可以是具体类或抽象类），那么在默认方法冲突时，类中定义的方法优先于接口中的默认方法。</li>
<li>这意味着类中的方法将覆盖接口中的默认方法，无论接口中的默认方法是新添加的还是继承的。</li>
</ul>
</li>
<li>接口冲突解决：<ul>
<li>如果一个类实现了多个接口，并且这些接口中包含了具有相同签名的默认方法，那么实现类必须显式地解决默认方法的冲突，通过覆盖默认方法并提供自己的实现。</li>
<li>实现类必须选择要使用的默认方法，或者完全忽略这些默认方法，并提供自己的实现。</li>
</ul>
</li>
<li>显式调用冲突的默认方法：<ul>
<li>如果实现类需要在自己的方法中调用冲突的默认方法，可以使用以下语法：<code>InterfaceName.super.methodName()</code>。</li>
<li>这样可以明确指定要调用的接口中的默认方法。</li>
</ul>
</li>
</ol>
<p>下面是一个解决默认方法冲突的示例：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">InterfaceA</span> {</span><br><span class="line">    <span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> {</span><br><span class="line">        System.out.println(<span class="string">"InterfaceA"</span>);</span><br><span class="line">    }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">InterfaceB</span> {</span><br><span class="line">    <span class="keyword">default</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> {</span><br><span class="line">        System.out.println(<span class="string">"InterfaceB"</span>);</span><br><span class="line">    }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyClass</span> <span class="keyword">implements</span> <span class="title class_">InterfaceA</span>, InterfaceB {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">method</span><span class="params">()</span> {</span><br><span class="line">        InterfaceA.<span class="built_in">super</span>.method(); <span class="comment">// 显式调用 InterfaceA 的默认方法</span></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<h1 id="接口回调"><a href="#接口回调" class="headerlink" title="接口回调"></a>接口回调</h1><h2 id="什么是接口回调"><a href="#什么是接口回调" class="headerlink" title="什么是接口回调"></a>什么是接口回调</h2><p>在Java中，接口回调是一种常见的设计模式，用于实现对象之间的通信和交互。它允许一个对象（回调接口的实现者）通过接口提供一组方法，然后将该对象的实例传递给另一个对象（调用者），以便在适当的时候调用回调方法来完成特定的操作。</p>
<p>接口回调的基本思想是通过接口来定义一组回调方法，以及一个具体类来实现这个接口并提供回调方法的具体实现。然后，其他对象可以持有实现了回调接口的对象，并在需要时调用其回调方法。</p>
<h2 id="接口回调的步骤"><a href="#接口回调的步骤" class="headerlink" title="接口回调的步骤"></a>接口回调的步骤</h2><ul>
<li>定义回调接口：首先，需要定义一个接口，其中声明了回调方法的签名。这些回调方法是由回调对象来实现和提供具体的实现逻辑。</li>
<li>实现回调接口：然后，创建一个类来实现回调接口，并实现接口中声明的回调方法。该类将提供具体的实现逻辑，即在回调方法中执行期望的操作。</li>
<li>注册回调对象：其他对象需要与回调对象进行通信。它们可以将回调对象的实例传递给调用者，并将其注册为回调对象。</li>
<li>调用回调方法：在合适的时机，调用者可以通过回调对象的引用调用回调方法来触发特定的操作。</li>
</ul>
<h2 id="接口回调的作用"><a href="#接口回调的作用" class="headerlink" title="接口回调的作用"></a>接口回调的作用</h2><ul>
<li>实现事件处理：当某个事件发生时，可以通过接口回调机制将事件传递给相应的处理对象，并执行相应的操作。例如，按钮点击事件、网络请求完成等。</li>
<li>实现插件机制：通过接口回调，可以实现插件式开发，允许在运行时动态地扩展和替换对象的功能。插件可以通过实现特定的回调接口来添加新的行为或修改现有行为。</li>
<li>解耦对象间的依赖：接口回调可以帮助对象之间解耦，减少它们之间的直接依赖关系。调用者只需要知道回调接口，并不需要知道具体的实现类，从而提高代码的可维护性和扩展性。</li>
<li>实现回调机制：接口回调是实现回调模式的一种常见方式。回调模式允许对象在不同的上下文中进行通信和交互，从而实现灵活的程序设计。</li>
</ul>
<p>通过接口回调，Java程序可以实现对象之间的松耦合通信，并提供灵活的扩展和定制化能力。它被广泛应用于事件处理、回调机制、插件化架构等场景。</p>
<h2 id="接口回调举例"><a href="#接口回调举例" class="headerlink" title="接口回调举例"></a>接口回调举例</h2><p>下面是一个简单的例子，演示了使用接口回调来处理按钮点击事件：</p>
<p>首先，定义一个回调接口 <code>ButtonClickListener</code>，其中声明了一个回调方法 <code>onClick()</code>：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">ButtonClickListener</span> {</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">onClick</span><span class="params">()</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>然后，创建一个按钮类 <code>Button</code>，其中包含一个 <code>ButtonClickListener</code> 类型的成员变量，并提供一个注册回调的方法 <code>setClickListener()</code> 和触发回调的方法 <code>click()</code>：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Button</span> {</span><br><span class="line">    <span class="keyword">private</span> ButtonClickListener clickListener;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setClickListener</span><span class="params">(ButtonClickListener listener)</span> {</span><br><span class="line">        <span class="built_in">this</span>.clickListener = listener;</span><br><span class="line">    }</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">click</span><span class="params">()</span> {</span><br><span class="line">        <span class="keyword">if</span> (clickListener != <span class="literal">null</span>) {</span><br><span class="line">            clickListener.onClick();</span><br><span class="line">        }</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>接下来，创建一个实现了 <code>ButtonClickListener</code> 接口的类 <code>MyButtonClickListener</code>，并实现回调方法 <code>onClick()</code>：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyButtonClickListener</span> <span class="keyword">implements</span> <span class="title class_">ButtonClickListener</span> {</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onClick</span><span class="params">()</span> {</span><br><span class="line">        System.out.println(<span class="string">"Button clicked!"</span>);</span><br><span class="line">        <span class="comment">// 执行其他操作...</span></span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>最后，在主程序中使用接口回调：</p>
<figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> {</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line">        <span class="type">Button</span> <span class="variable">button</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Button</span>();</span><br><span class="line">        <span class="type">ButtonClickListener</span> <span class="variable">listener</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">MyButtonClickListener</span>();</span><br><span class="line">        button.setClickListener(listener);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 模拟用户点击按钮</span></span><br><span class="line">        button.click();</span><br><span class="line">    }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Java%E5%9F%BA%E7%A1%80-%E6%8E%A5%E5%8F%A3/" rel="tag"># -Java基础 -接口</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/06/15/Java%EF%BC%9A%E7%BB%A7%E6%89%BF/" rel="prev" title="Java：继承">
                  <i class="fa fa-chevron-left"></i> Java：继承
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/06/28/Java%EF%BC%9A%E5%B8%B8%E7%94%A8%E5%AE%9E%E7%94%A8%E7%B1%BB/" rel="next" title="">
                   <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">鸣蜩十七</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
